<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" label-width="77px" class="ele-form-search" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="8" :md="12">
            <el-form-item label="创建时间:">
              <el-date-picker style="width: 100%" v-model="where.time_range" type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss" label-format="yyyy-MM-dd HH:mm:ss" range-separator="至"
                start-placeholder="开始日期" end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="游戏名称">
              <el-input :placeholder="'请输入游戏名称'" v-model="where.game_name"></el-input>
            </el-form-item>
          </el-col>

          <el-col :lg="4" :md="12">
            <el-form-item label="状态:">
              <el-select clearable v-model="where.status" placeholder="请选择状态" class="ele-fluid">
                <el-option label="全部" value="" />
                <el-option label="待上架" value="1" />
                <el-option label="已上架" value="2" />
                <el-option label="已下架" value="3" />
                <el-option label="上架即将开始" value="4" />
                <el-option label="上架进行中" value="5" />
                <el-option label="上架已结束" value="6" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button size="small" type="primary" icon="el-icon-plus" class="ele-btn-icon" @click="openEdit(null)"
            v-if="permission.includes('sys:boxtask:add')">配置任务
          </el-button>
        </template>
        <template slot="avatar" slot-scope="{ row }">
          <el-image style="width: 30px; height: 30px" :src="row.icon" fit="fit"></el-image>
        </template>
        <template slot="action" slot-scope="{ row }">
          <el-link type="primary" :underline="false" icon="el-icon-edit" @click="openEdit(row)"
            v-if="permission.includes('sys:boxtask:edit')">编辑
          </el-link>
          <el-link type="danger" :underline="false" icon="el-icon-turn-off" @click="status(row)">{{ row.status === 1 ?
            "禁用" : "启用" }}
          </el-link>
          <el-link type="primary" :underline="false" @click="openCdk(row.id)" icon="el-icon-shopping-bag-1">任务cdk列表
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <GameCateEdit :TaskData=current :gameNames=gameNames :visible.sync=showEdit @done="reload" />
    <!-- CDK弹窗 -->
    <task-cdk :visible.sync="showCdk" :taskId = "taskId" @done="reload" ref="taskCdkList" />
  </div>
</template>
<script>
import axios from "axios";
import { mapGetters } from "vuex";
import GameCateEdit from "./game-task-edit.vue";
import TaskCdk from "./task-cdk.vue"

export default {
  name: "BoxGame",
  components: { GameCateEdit, TaskCdk },
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      // 表格数据接口
      url: "/boxGametaskConfig/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left"
        },
        {
          prop: "id",
          label: "序号",
          width: 100,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: "game_name",
          label: "游戏名称",
          minWidth: 180,
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "server_name",
          label: "区服名称",
          minWidth: 100,
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "task_type",
          label: "任务名称",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          formatter: (row, column, cellValue) => {
            if (cellValue == 1) {
              return "元气任务";
            } else if (cellValue == 2) {
              return "武功任务";
            } else if (cellValue == 3) {
              return "充值任务";
            } else if (cellValue == 4) {
              return "年龄任务";
            } else {
              return "---";
            }
          }
        },
        {
          prop: "task_condition",
          label: "任务条件",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "sort",
          label: "排序(数字越大,权重越高)",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "status",
          label: "状态",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          formatter: (row, column, cellValue) => {
            if (cellValue == 1) {
              return "启用";
            } else if (cellValue == 2) {
              return "禁用";
            }
          }
        },
        {
          prop: "info",
          label: "备注",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "start_time",
          label: "开始时间",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "end_time",
          label: "结束时间",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "create_time",
          label: "创建时间",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          columnKey: "action",
          label: "操作",
          width: 260,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: {},
      // 当前行id数据
      gameRowID: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 是否显示计费点管理弹窗
      showEditGameChargePoint: false,
      gameStatus: {},
      //游戏列表
      gameNames: [],
      //分页功能
      pageSize: 15, //每页显示条数  6条刚好
      currentPage: 1, //当前页数
      pageCount: 6, //按钮数，超过时会折叠
      pageTotal: 0, //总条数

      //CDK相关
      taskId: 0,
      showCdk: false
    };
  },
  // created() {
  //   this.loadGameNames();
  // },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 显示编辑 */
    openEditGameChargePoint(row) {
      this.gameRowID = row.id;
      this.current = row;
      this.showEditGameChargePoint = true;
    },

    status(row) {
      if (row.status === 1) {
        this.gameStatus = { status: 2, id: row.id };
      } else {
        this.gameStatus = { status: 1, id: row.id };
      }
      this.loading = true;
      this.$http
        .post("/boxGametaskConfig/status", this.gameStatus)
        .then(res => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.$emit("done");
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({ lock: true });
      this.$http
        .post("/game/delete", { id: row.id })
        .then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的游戏吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          const loading = this.$loading({ lock: true });
          this.$http
            .post("/game/delete", { id: this.selection.map(d => d.id) })
            .then(res => {
              loading.close();
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.reload();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              loading.close();
              this.$message.error(e.message);
            });
        })
        .catch(() => { });
    },

    // //获取盒子游戏列表
    // loadGameNames() {
    //   axios
    //     .post("/boxGame/index", {
    //       page: this.currentPage,
    //       limit: this.pageSize
    //     })
    //     .then(res => {
    //       if (res.data.code != 0) {
    //         console.log("获取数据失败！");
    //         return;
    //       }
    //       this.gameNames = res.data.data;
    //       this.pageTotal = res.data.count;
    //     });
    // },
    changeNumber(val) {
      axios
        .post("/boxGame/index", { page: val, limit: this.pageSize })
        .then(res => {
          this.gameNames = res.data.data;
        });
    },
    //打开CDK列表
    openCdk(id) {
      this.showCdk = true
      this.$nextTick(() => {
        if(id){
          this.taskId = id
          this.$refs.taskCdkList.taskId = id
          this.$refs.taskCdkList.open()     
        }       
      })
      
    },
  }
};
</script>

<style scoped></style>
